package com.qk.management.controller;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.management.service.impl.RoleServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/roles")
@SuppressWarnings("all")
public class RoleController {
    @Autowired
    private RoleServiceImpl roleService;

    @GetMapping("/list")
    public Result list() {
        log.info("查询所有角色");
        return Result.success(roleService.list());
    }
    @GetMapping
    public Result page(@RequestParam(name = "name", required = false)String name,
                       @RequestParam(name = "label", required = false) String label,
                       @RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
                       @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer pageSize) {
        PageResult<Role> pageResult = roleService.page(name, label,page, pageSize);
        log.info("分页查询角色,参数:角色名称={},角色标识={}，当前页码={},每页大小={}", name,label, page, pageSize);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result addRole(@RequestBody Role role) {
        roleService.insert(role);
        log.info("新增角色:{}", role);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        log.info("查询角色id:{}", id);
        Role role = roleService.selectById(id);
        return Result.success(role);
    }

    @PutMapping()
    public Result update(@RequestBody Role role) {
        log.info("修改角色:{}", role);
        roleService.update(role);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        log.info("删除角色id:{}", id);
        roleService.deleteById(id);
        return Result.success();
    }




}
